/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.whu.weibo_souhu.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.HttpEntity;
import org.whu.weibo_souhu.login.Cookie;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;



/**
 *
 * @author Administrator
 */
public class URL implements Runnable {
    
    public String url=new String();
    public Cookie cookie=null;
    public String result=new String();
    public Integer num=1;
    
    public URL(String url,Cookie cookie)
    {
        this.cookie=cookie;
        this.url=url;
    }
    
    public String gethtml() 
    {
        String result=new String();
        HttpClient httpclient=new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000); //请求超时
        httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000); //读取超时
        HttpGet httpget = new HttpGet(url);
        
        httpget.setHeader("Accept", "*/*");
        httpget.setHeader("Accept-Encoding","gzip,deflate");
        httpget.setHeader("Accept-Language", "zh-CN");
        httpget.setHeader("Connection","Keep-Alive");
    //    httpget.setHeader("Cookie",cookie.toString());
    //    httpget.setHeader("Host","z.t.sohu.com");
   //     httpget.setHeader("Referer","http://t.sohu.com/new_index");
       // httpget.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)");
        try {
            HttpResponse response=httpclient.execute(httpget);
            if(response.getStatusLine().getStatusCode()==200)
            {
                 HttpEntity entity=response.getEntity();
                
                InputStream content = entity.getContent();
                
                InputStreamReader bf = new InputStreamReader(content,"gb2312");
                BufferedReader br= new BufferedReader(bf);
                String readLine = br.readLine();
               System.out.print(readLine);
              
                
                
            //    StringEntity my_entity=new StringEntity(EntityUtils.toString(entity, "GBK"));
              //  result=EntityUtils.toString(my_entity);
        //        result = new String(new String(EntityUtils.toString(entity)).getBytes("GBK"),"utf-8");

               result=new String(readLine.getBytes("gbk"),"utf-8");
            }
               
               // result=EntityUtils.toString(response.getEntity());
            //result = new String(result.getBytes("GB2312"),"UTF-8");
//            System.out.println(result);
        } catch (IOException ex) {
            num++;
            if(num>=10)
            {
                System.out.println("重试次数超过限制,增加间隔时间");
                try {
                    Thread.sleep(10000 * num);
                } catch (InterruptedException ex1) {
                    Logger.getLogger(URL.class.getName()).log(Level.SEVERE, null, ex1);
                }
                    result= this.gethtml();
            }
            if(num>=30)
                return "";
            System.out.println("URL模块出错，请重试");
            Logger.getLogger(URL.class.getName()).log(Level.SEVERE, null, ex);
            result= this.gethtml();
        }
        finally{
        return result;
         }
    }
    
    public void run()
    {
        result=this.gethtml();
    }
    
}
